package cn.wowjoy.pda.android_pharmacy.ui.activity.staticH.check;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityStaticCheckHandDetailBinding;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.inHospitalPatientInfo.InHospitalPatientBeen;
import cn.wowjoy.pda.android_pharmacy.mvvm.presenter.staticH.StaticCheckHandDetailPresenter;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.staticH.StaticCheckHandDetailViewModel;
import gear.yc.com.gearlibrary.rxjava.rxbus.RxBus;

public class StaticCheckHandDetailActivity extends BaseActivity<ActivityStaticCheckHandDetailBinding,StaticCheckHandDetailViewModel,StaticCheckHandDetailPresenter> {
    private InHospitalPatientBeen check;
    private MDialog mDialog;
    public static final int CLOSE=1;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CLOSE:
                    //TODO:3秒后自动跳转
//                        CreateDialog.dismiss(InHospitalCheckHandDetailActivity.this);
//                        mDialog.dismiss();
                    Intent intent=new Intent();
                    intent.putExtra("remove",check);
                    setResult(Contants.RESULT_HAND_SUCCESS_CODE,intent);
                    finish();
                    break;
            }
        }
    };

    @Override
    protected void init(Bundle savedInstanceState) {

        binding.setViewModel(viewModel);
        binding.setPresenter(presenter);

        binding.hosptalResultTitle.titleTextTv.setText("药品审核");
        binding.hosptalResultTitle.titleBackLl.setVisibility(View.VISIBLE);
        binding.hosptalResultTitle.titleBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(Contants.RESULT_HAND_FAIL_CODE);
                finish();
            }
        });
        binding.hosptalResultTitle.titleBackTv.setText("");
        check = (InHospitalPatientBeen) this.getIntent().getExtras().getSerializable("check");
        if(null != check){
            binding.setModel(check);
            if(null != check.getSFGWPB() && !"".equals(check.getSFGWPB())){
                if(Integer.parseInt(check.getSFGWPB()) == 1){
                    binding.medicineNameHosptalResult.setTextColor(getResources().getColor(R.color.red));
                }else{
                    binding.medicineNameHosptalResult.setTextColor(getResources().getColor(R.color.text_undanger));
                }
            }
            binding.medicineNameHosptalResult.setText(check.getZZYPMC()+"   "+check.getZZYPGG());
            binding.numFactHosptalResult.setText(check.getCFYPSL());
            binding.numFactHosptalResult.setSelection(binding.numFactHosptalResult.getText().length());
        }
        binding.btSureHosptalResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.numFactHosptalResult.getText().length() == 0) {
                    new MyToast(StaticCheckHandDetailActivity.this).showinfo("请输入取药数量");
                } else {
                    if (presenter.check(binding.numShouldHosptalResult.getText().toString().trim(), binding.numFactHosptalResult.getText().toString().trim())) {
                        //TODO:弹框 药品Name
                        if(null != check){
                            RxBus.getInstance().post(Contants.STATIC_CHECK_MEDICINE_BY_HAND, check);
                        }
                        mHandler.sendEmptyMessage(CLOSE);
                    }else{
                        binding.numFactHosptalResult.setText("");
                    }
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        setResult(Contants.RESULT_HAND_FAIL_CODE);
        super.onBackPressed();
    }
    @Override
    protected void onDestroy() {
        RxBus.getInstance().unRegister(this);
        super.onDestroy();
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_static_check_hand_detail;
    }

    @Override
    protected Class<StaticCheckHandDetailPresenter> getPresenterClass() {
        return StaticCheckHandDetailPresenter.class;
    }

    @Override
    protected Class<StaticCheckHandDetailViewModel> getViewModelClass() {
        return StaticCheckHandDetailViewModel.class;
    }
}
